package com.rock.code.province.tj.busi.demo;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;


/**
 * 服务端
 */
public class ServerByJson {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        ServerSocket ss = new ServerSocket(8080);
                        System.out.println("启动服务器....");
                        final Socket s = ss.accept();


                        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                        //读取客户端发送来的消息
                        String mess = br.readLine();
                        System.out.println("我收到了客户端的东西" + mess);
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                while (true) {
                                    try {
                                        Thread.sleep(10 * 1000);// 10s发送一次心跳
                                        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                                        String jsonParams = "254109P01_02508562{\"time\":\"2016-04-18 20:00:01\",\"data\":[{\"dim\":\"con\",\"v\":\"85.71\",\"tt\":\"02\",\"code\":\"22402\"}, {\"dim\":\"con\",\"v\":\"60.0\",\"tt\":\"02\",\"code\":\"43411\"}]}";
                                        bw.write(jsonParams + "\n");
                                        bw.flush();
//                br.close();
//                s.close();
//                ss.close();
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }).start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }

}
